﻿using System;
using System.Data;
using IntelligentStorWpfApp.Interface;
using IntelligentStorWpfApp.Model;
using IntelligentStorWpfApp.Helper;
using MySql.Data.MySqlClient;

namespace IntelligentStorWpfApp.Services
{
    public class PositionServices : PositionInterface
    {
        string sqlStr="";
        public bool Add(Position position)
        {
            sqlStr = "INSERT INTO c_position "+
                "(cabinetNum, positionNum) VALUES (@cabinetNum, @positionNum);";
            MySqlParameter[] paras =
            {
                new MySqlParameter ("@cabinetNum",position.CabinetNum),
                new MySqlParameter ("@positionNum",position.PositionNum)
            };

            return DBHelper.ExecuteNonQuery(sqlStr, paras);
        }

        public bool Delete(int Id)
        {
            sqlStr = "delete from c_position where Id=@Id;";
            MySqlParameter[] paras =
             {
                new MySqlParameter ("@Id",Id)
            };

            return DBHelper.ExecuteNonQuery(sqlStr, paras);
        }

        public DataTable GetPositions(Position position)
        {
            sqlStr = "select t.Id as ID,t.positionNum as PositionNum,p.StoreStatus as StoreStatus"
                +" from c_position t left join c_status p on t.Id=p.PositionId"
                +" where t.cabinetNum=@cabinetNum;";
            MySqlParameter[] paras =
            {
                new MySqlParameter ("@cabinetNum",position.CabinetNum)
            };
            return DBHelper.ExecuteDataSet(sqlStr, paras);
        }



        public DataTable GetCabinetNums()
        {
            sqlStr = "select distinct cabinetNum from c_position;";
            return DBHelper.ExecuteDataSet(sqlStr, null);

        }

        public bool Update(Position position)
        {
            throw new NotImplementedException();
        }

        public DataTable getSinglePosition(Position position)
        {
            sqlStr = "select * from c_position "
                + " where cabinetNum=@cabinetNum and positionNum=@positionNum;";
            MySqlParameter[] paras =
            {
                new MySqlParameter ("@cabinetNum",position.CabinetNum),
                new MySqlParameter ("@positionNum",position.PositionNum)
            };
            return DBHelper.ExecuteDataSet(sqlStr, paras);
        }
    }
}
